package artid.com.arttogo.activity;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.HitTestResult;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import artid.com.arttogo.R;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.bean.AddOrder;
import artid.com.arttogo.bean.ConfirmOrder;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.utils.ToastUtil;

/**
 * TODO<请描述这个类是干什么的> 确认订单
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class EnsureOrderActivity extends BaseActivity implements
        OnClickListener {

    private LinearLayout mPerson_main_rela;
    private RelativeLayout mEnorder_viewtop;// 显示地址
    private TextView mEnorder_name;
    private TextView mEnorder_phone;
    private TextView mEnorder_adderss;
    private ImageView mEnorder_img;
    private TextView mEnorder_artname;
    private TextView mEnorder_cate;
    private TextView mEnorder_size;
    private TextView mEnorder_price1;
    private TextView mText_name;
    private RelativeLayout mItem_view_date;
    private TextView mText_personcenter_date;
    private TextView mEnorder_price2;
    private TextView mEnorder_buy;
    private View addAddress;// 添加地址
    String artName, cate, size, price, imgurl, goodid;
    String addressId = "";
    String orderid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ensure_order);
        goodid = getIntent().getExtras().getString("id");
        mContext = EnsureOrderActivity.this;
        ArtToGoApp.getInstance().addMallActivity(EnsureOrderActivity.this);
        mPerson_main_rela = (LinearLayout) findViewById(R.id.person_main_rela);
        mEnorder_viewtop = (RelativeLayout) findViewById(R.id.enorder_viewtop);
        mEnorder_viewtop.setOnClickListener(this);
        mEnorder_name = (TextView) findViewById(R.id.enorder_name);
        mEnorder_phone = (TextView) findViewById(R.id.enorder_phone);
        mEnorder_adderss = (TextView) findViewById(R.id.enorder_adderss);
        mEnorder_img = (ImageView) findViewById(R.id.enorder_img);
        mEnorder_artname = (TextView) findViewById(R.id.enorder_artname);
        mEnorder_cate = (TextView) findViewById(R.id.enorder_cate);
        mEnorder_size = (TextView) findViewById(R.id.enorder_size);
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(this);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("确认订单");
        mEnorder_price1 = (TextView) findViewById(R.id.enorder_price1);

        mText_name = (TextView) findViewById(R.id.text_name);
        mItem_view_date = (RelativeLayout) findViewById(R.id.item_view_date);
        mText_personcenter_date = (TextView) findViewById(R.id.text_personcenter_date);
        mEnorder_price2 = (TextView) findViewById(R.id.enorder_price2);
//		mEnorder_price2.setText(price);
        mEnorder_buy = (TextView) findViewById(R.id.enorder_buy);
        mEnorder_buy.setOnClickListener(this);
        addAddress = (View) findViewById(R.id.enorder_viewtop_add);
        addAddress.setOnClickListener(this);

    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        reqData(goodid);

    }
    void reqData(String id) {
        VolleyBasic.getInstance(EnsureOrderActivity.this).post(
                Cache.confirmOrder, VolleyMap.getInstance().confirmOrder(id),
                ConfirmOrder.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        ConfirmOrder item = (ConfirmOrder) vd.getData();

                        Glide.with(EnsureOrderActivity.this).load(item.getGoods_image()+ AppUtil.picWidthSmallUrl)
                                .asBitmap().centerCrop()
                                .placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
                                .into(new MyBitmapImageViewTarget(mEnorder_img));
                        mEnorder_artname.setText(item.getGoods_name());
                        mEnorder_cate.setText(item.getGc_name());
                        mEnorder_size.setText(item.getGoods_size());
                        mEnorder_price1.setText("¥" + item.getGoods_price());
                        mEnorder_price2.setText("¥" + item.getGoods_price());
                        price = item.getGoods_price();
                        if (!TextUtils.isEmpty(item.getAdressinfo())) {
                            addAddress.setVisibility(View.GONE);
                            mEnorder_viewtop.setVisibility(View.VISIBLE);
                            mEnorder_name.setText(item.getTrue_name());
                            mEnorder_phone.setText(item.getMob_phone());
                            mEnorder_adderss.setText(item.getAdressinfo());
                            addressId = item.getAddress_id();

                        }

                        return;
                        // }
                        // ArtistInfo list = (ArtistInfo)vd.getData();
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    private void requestGradeList() {
        if (TextUtils.isEmpty(addressId)) {
            return;
        }
        if (TextUtils.isEmpty(mEnorder_name.getText().toString())
                || TextUtils.isEmpty(mEnorder_phone.getText().toString())
                || TextUtils.isEmpty(mEnorder_adderss.toString())) {
            ToastUtil.toast(EnsureOrderActivity.this, "确保收货地址信息完整");
            return;
        }

        VolleyBasic.getInstance(EnsureOrderActivity.this).post(Cache.addOrder,
                VolleyMap.getInstance().addOrder(addressId, goodid, price),
                AddOrder.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        AddOrder item = (AddOrder) vd.getData();
                        if (!TextUtils.isEmpty(item.getOrder_sn())) {
                            Intent intent = new Intent(EnsureOrderActivity.this,
                                    ChoicePayActivity.class);
                            if (TextUtils.isEmpty(item.getOrder_id())) {
                                ToastUtil.toast(EnsureOrderActivity.this, "商品暂时无法操作");
                                return;
                            }
                            intent.putExtra("id", item.getOrder_id());
                            startActivity(intent);
                        }

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                    }
                });
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        backAlert();
    }
    void backAlert(){
        AlertDialog alertDialog = new AlertDialog.Builder(mContext).setMessage("喜欢的作品，怎可轻易留给Ta人")
                .setPositiveButton("我再想想", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    }
                }).setNegativeButton("去意已决", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        finish();
                    }
                }).show();

    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.enorder_buy:
                requestGradeList();
                break;
            case R.id.enorder_viewtop:
                Intent intent2 = new Intent(EnsureOrderActivity.this,
                        ChoiceAddressActivity.class);

                startActivity(intent2);
                break;
            case R.id.enorder_viewtop_add:
                Intent intent = new Intent(EnsureOrderActivity.this,
                        ChoiceAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.back_btn:
                backAlert();
                break;
            default:
                break;
        }
    }



}
